package com.cheng.xbsc.service.impl;

import com.cheng.xbsc.entity.Car;
import com.cheng.xbsc.entity.Goods;
import com.cheng.xbsc.entity.Image;
import com.cheng.xbsc.entity.vo.Result;
import com.cheng.xbsc.mapper.CarMapper;
import com.cheng.xbsc.mapper.GoodsMapper;
import com.cheng.xbsc.mapper.ImageMapper;
import com.cheng.xbsc.service.GoodsService;
import com.cheng.xbsc.util.LoginUserUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 刘文铖
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;


    @Override
    public Result findDetail(Integer goodsId) {
        //查看缓存中是否有记录
        String tag = (String) redisTemplate
                .opsForValue()
                .get("goodsIdAndUserId:" + goodsId + "," + LoginUserUtil.getUserId());
        //如果没有记录查看数+1
        if (StringUtils.isEmpty(tag)) {
            goodsMapper.updateLook(goodsId);
            //设置一个值在缓存中作为标记
            redisTemplate.opsForValue()
                    .set("goodsIdAndUserId:" + goodsId + "," + LoginUserUtil.getUserId(), "tag", 1, TimeUnit.MINUTES);
        }
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        List<Image> imageList = imageMapper.findByGoodsId(goodsId);
        int isCollect = goodsMapper.countByUidAndGid(LoginUserUtil.getUserId(), goodsId);
        Map<String, Object> map = new HashMap<>();
        map.put("goods", goods);
        map.put("imageList", imageList);
        map.put("isCollect", isCollect);
        return new Result(true, "查询成功", map);
    }

    @Override
    public Result doCollect(Integer goodsId) {
        Integer userId = LoginUserUtil.getUserId();
        int isCollect = goodsMapper.countByUidAndGid(userId, goodsId);
        //判断是否收藏过
        if (isCollect > 0) {
            //取消收藏
            goodsMapper.deleteByUidAndGid(userId, goodsId);
            return new Result(true, "取消收藏成功", goodsMapper.countByUidAndGid(userId, goodsId));
        }
        //收藏商品
        goodsMapper.saveByUidAndGid(userId, goodsId);
        return new Result(true, "收藏成功", goodsMapper.countByUidAndGid(userId, goodsId));
    }

    @Override
    public void saveCar(Car car) {
        Integer userId = LoginUserUtil.getUserId();
        Car dbCar = carMapper.findByUidAndGidAndStatus(userId, car.getGoodsId(), "0");
        //判断原来购物车中是否有这件商品
        if (dbCar != null) {
            //有这件商品，更新数量
            dbCar.setCreateTime(new Date());
            dbCar.setGoodNum(dbCar.getGoodNum() + car.getGoodNum());
            carMapper.updateByPrimaryKeySelective(dbCar);
        } else {
            //没有这件商品，新建一个
            car.setCreateTime(new Date());
            car.setUserId(userId);
            car.setCarStatus("0");
            carMapper.insert(car);
        }
    }

    @Override
    public PageInfo<Goods> findProduct(Integer pageNum, Integer productId) {
        PageHelper.startPage(pageNum, 5);
        Goods goods = new Goods();
        goods.setProductId(productId);
        return new PageInfo<Goods>(goodsMapper.select(goods));
    }

    @Override
    public PageInfo<Goods> searchTitle(Integer pageNum, String title) {
        PageHelper.startPage(pageNum, 5);
        return new PageInfo<Goods>(goodsMapper.findByTitle("%" + title + "%"));
    }

    @Override
    public PageInfo<Goods> findCollect(Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        return new PageInfo<Goods>(goodsMapper.findCollect(LoginUserUtil.getUserId()));
    }
}
